﻿
using System ;

using MskEdit.Msk.Representation ;


namespace MskEdit.Msk.Model
{
    public class MskPageKey {
        
        private static  MskPageKey  nullPageKey_ = new MskPageKey() ;
        private         MskLanguage Language_ ;
        private         MskPageCode PageCode_ ;
        
        
        public static MskPageKey TryFrom( string line, MskPageKey fallbackValue ) {
        
            MskLanguage mskLanguage = MskLanguage.TryFrom( line, null ) ;
            if ( mskLanguage == null )
                return fallbackValue ;
 
            MskPageCode mskPageCode = MskPageCode.TryFrom( line, null ) ;
            if ( mskPageCode == null )
                return fallbackValue ;

            return new MskPageKey( mskLanguage, mskPageCode ) ;
        }
        

        public MskPageKey() {
            Language_ = MskLanguage.NullLanguage ;
            PageCode_ = MskPageCode.NullPageCode ;
        }


        public MskPageKey( MskLanguage language, MskPageCode pageCode ) {
            Language_ = language ;
            PageCode_ = pageCode ;
        }


        public bool Matches( MskPageKey pageKey ) {
            return 
                ( Language_ == pageKey.Language_ ) && 
                ( PageCode_ == pageKey.PageCode_ ) ;
        }


        public bool Matches( String line ) {
            return 
                Language_.Matches( line ) && 
                PageCode_.Matches( line ) ;
        }

        
        public static MskPageKey NullPageKey {
            get { return nullPageKey_ ; }
        }
        

        public MskLanguage Language {
            get { return Language_ ; }
            set { Language_ = value ; }
        }


        public MskPageCode PageCode {
            get { return PageCode_ ; }
            set { PageCode_ = value ; }
        }


        override public String ToString() {
        
            string pageKeyAsString = 
                Language_.ToString() + 
                "." + 
                PageCode_.Number.ToString() ;
                
            return pageKeyAsString ;
        }


        public override bool Equals( object obj ) {
        
            MskPageKey that = ( MskPageKey ) obj ;
            
            if ( ! that.Language.Equals( this.Language ) )
                return false ;
                
            if ( ! that.PageCode.Equals( this.PageCode ) )
                return false ;
                
            return true ;
        }


        public override int GetHashCode() {
        
            var languageHashCode = this.Language.GetHashCode() ;
            var pageCodeHashCode = this.PageCode.GetHashCode() ;
            
            var hashCode = languageHashCode ^ pageCodeHashCode ;
            
            return hashCode ;
        }
        
    }
}
